package client

import (
	"tio-proxy-agent/log"
)

type BsPacket struct {
	Type byte
	Body []byte
}

func (packet *BsPacket) GetType() byte {
	return packet.Type
}

func (packet *BsPacket) GetBody() []byte {
	return packet.Body
}

type ConnectSuccessBody struct {
	Token string `json:"token"`
	Host  string `json:"host"`
	BsId  string `json:"bsId"`
}

func (body *ConnectSuccessBody) Handler() {
	log.SugarLogger.Info("动态域名为:", body.Host)
}

type TcpBody struct {
	Bytes []byte `json:"bytes"`
	BsId  string `json:"bsId"`
}

func (body *TcpBody) Handler() {
	packet := &BsPacket{
		Type: 98,
		Body: body.Bytes,
	}
	ChannelManage[body.BsId].SendData <- packet
	//ChannelManage[body.BsId].SendByteBuffer <- bytes.NewBuffer(body.Bytes)
}

type TcpTunnelBody struct {
	BsId string `json:"bsId"`
}

func (body *TcpTunnelBody) Handler() {

	if ChannelManage[body.BsId] == nil {
		config := new(GioClientForwardConfig)
		config.BsId = body.BsId
		gioClientForward := NewGioClientForward(config, nil, body.BsId)
		go func() {
			gioClientForward.Connect(TunnelInfo.TargetHost, TunnelInfo.TargetPort)
		}()
		<-gioClientForward.isInitOk
	}

}
